package application.gui.clientWindow;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.PersonData;
import application.gui.main.startWindow.Homepage;

public class ClientHomePage extends JFrame {

	public ClientHomePage(PersonData loggedUser, Homepage f) {
		loggedInUser = loggedUser;
		startPage = f;
		try {
			db = new Communication("inzynier_admin", "admin");
		}catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		} finally {
			initComponents();
			setListeners();
		}
	}

	private void initComponents() {
		setSize(800, 600);
		setTitle("ePaczka - panel Klienta");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setLayout(null);

		rightPanel = new JPanel();
		rightPanel.setLayout(null);
		rightPanel.setBounds(620, 0, 180, 120);
		add(rightPanel);
		logoutButton = new JButton("Wyloguj");
		rightPanel.add(logoutButton);
		logoutButton.setBounds(0, 5, 100, 30);
		userInfo = new JTextArea(loggedInUser.getName()+" "+loggedInUser.getSurname()+"\n"+loggedInUser.geteMail()+"\n");
		userInfo.setEnabled(false);
		if (loggedInUser.getRights()==1)
			userInfo.append("Klient");
		else if (loggedInUser.getRights()==2)
			userInfo.append("Pracownik");
		else
			userInfo.append("Administrator");
		rightPanel.add(userInfo);
		userInfo.setBounds(0, 30, 150, 100);
		userInfo.setBackground(null);

		createAndShowStatusPanel();
		add(deliveryPanel);
		deliveryPanel.setBounds(200, 120, 600, 360);
		mainPanel = new JPanel();
		add(mainPanel);
		mainPanel.setBounds(200, 120, 600, 480);
		welcomeLabel = new JLabel("Witaj " + loggedInUser.getName()
				+ ". Wybierz interesującą Cię opcję w menu.");
		mainPanel.add(welcomeLabel);
		welcomeLabel.setBounds(0, 60, 600, 30);
		parcelInfo = new JLabel();
		mainPanel.add(parcelInfo);
		parcelInfo.setBounds(0, 130, 600, 90);
		parcelInfo.setVisible(false);
		parcelInfo.setFont(new Font("Arial", Font.BOLD, 15));
		logoutButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				startPage.setVisible(true);
				startPage.clearFields();
				dispose();
			}
		});
		createMenuPanel();
		createShowParcelPanel();
		createShowDataPanel();
		newParcelPanel = new AddNewParcelPanel(this, db, loggedInUser);
		add(newParcelPanel);
		newParcelPanel.setBounds(200, 120, 600, 480);
		newParcelPanel.setVisible(false);
	}

	private void createMenuPanel() {
		menuPanel = new JPanel();
		menuPanel.setLayout(null);
		menuPanel.setBounds(0, 0, 200, 600);
		add(menuPanel);
		addParcel = new JButton("Nadaj paczkę");
		menuPanel.add(addParcel);
		addParcel.setBounds(0, 100, 200, 30);
		showMyParcel = new JButton("Wyświetl moje paczki");
		menuPanel.add(showMyParcel);
		showMyParcel.setBounds(0, 130, 200, 30);
		parcelHistory = new JButton("Historia nadań");
		menuPanel.add(parcelHistory);
		parcelHistory.setBounds(0, 160, 200, 30);
		changeData = new JButton("Zmień dane osobowe");
		menuPanel.add(changeData);
		changeData.setBounds(0, 190, 200, 30);
		checkDeliveryStatusButton = new JButton("Sprawdź status przesyłki");
		menuPanel.add(checkDeliveryStatusButton);
		checkDeliveryStatusButton.setBounds(0, 220, 200, 30);

	}

	private void createShowParcelPanel() {
		showParcelPanel = new ActiveParcelsPanel(this, db, loggedInUser, true);
		add(showParcelPanel);
		showParcelPanel.setBounds(200, 120, 560, 440);
		showParcelPanel.setVisible(false);
	}
	
	private void createShowDataPanel(){
		dataPanel = new MyDataPanel(this, loggedInUser);
		add(dataPanel);
		dataPanel.setBounds(200, 120, 560, 440);
		dataPanel.setVisible(false);
	}

	private void setListeners() {
		addParcel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addNewParcel();
				if (mainPanel.isVisible())
					mainPanel.setVisible(false);
				if (showParcelPanel.isVisible())
					showParcelPanel.setVisible(false);
				if (deliveryPanel.isVisible())
					deliveryPanel.setVisible(false);
				if (dataPanel.isVisible())
					dataPanel.setVisible(false);
			}
		});

		showMyParcel.addActionListener(showActiveParcelListener);

		parcelHistory.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				historyParcels();
				if (mainPanel.isVisible())
					mainPanel.setVisible(false);
				if (newParcelPanel.isVisible())
					newParcelPanel.setVisible(false);
				if (deliveryPanel.isVisible())
					deliveryPanel.setVisible(false);
				if (dataPanel.isVisible())
					dataPanel.setVisible(false);
			}
		});

		checkDeliveryStatusButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (mainPanel.isVisible())
					mainPanel.setVisible(false);
				if (newParcelPanel.isVisible())
					newParcelPanel.setVisible(false);
				if (dataPanel.isVisible())
					dataPanel.setVisible(false);
				if (showParcelPanel.isVisible())
					showParcelPanel.setVisible(false);
				if (!deliveryPanel.isVisible()) {
					deliveryPanel.setVisible(true);
				}

			}
		});
		checkDelivery.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					db = new Communication("inzynier_admin", "admin");
					String status = db.getParcelStatus(Integer
							.parseInt(deliveryInput.getText()));
					deliveryInfo.setVisible(true);
					deliveryInfo.setText("Status: " + status);
				} catch (ClassNotFoundException e) {
					
					e.printStackTrace();
				} catch (SQLException e) {
					
					e.printStackTrace();
				}

			}
		});
		changeData.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (mainPanel.isVisible())
					mainPanel.setVisible(false);
				if (newParcelPanel.isVisible())
					newParcelPanel.setVisible(false);
				if (showParcelPanel.isVisible())
					showParcelPanel.setVisible(false);
				if (deliveryPanel.isVisible()) {
					deliveryPanel.setVisible(false);
				}
				if (!dataPanel.isVisible())
					dataPanel.setVisible(true);
			}
		});
	}

	private void addNewParcel() {
		if (!newParcelPanel.isVisible()) {
			newParcelPanel.setVisible(true);
			newParcelPanel.clearFields();
			repaint();
			validate();
		}
	}

	private void showParcels() {
		if (!showParcelPanel.isVisible()) {
			showParcelPanel.setVisible(true);
		}
		showParcelPanel.refresh(true);
		repaint();
		validate();
	}

	private void historyParcels() {
		if (!showParcelPanel.isVisible()) {
			showParcelPanel.setVisible(true);
		}
		showParcelPanel.refresh(false);
		repaint();
		validate();
	}

	public void registerMsg(int nr) {
		if (nr == 1) {
			newParcelPanel.setVisible(false);
			mainPanel.setVisible(true);
			parcelInfo.setText("");
		}
		// Edit parcel
		if (nr == 2) {
			showParcelPanel.setVisible(false);
			newParcelPanel.setVisible(true);
			setVisible(true);
		}
		//change data
		if(nr ==3){
			dataPanel.setVisible(false);
			mainPanel.setVisible(true);
		}
		if (nr == 4){
			parcelInfo.setVisible(true);
			parcelInfo.setText("Dane zostały zmienione");
			dataPanel.setVisible(false);
			mainPanel.setVisible(true);
		}
		repaint();
		validate();
	}

	public void registerMsg(int success, int parcelNumber) {
		// TODO Auto-generated method stub
		parcelInfo.setVisible(true);
		parcelInfo.setText("Paczka została nadana do systemu, jej numer to: "
				+ String.valueOf(parcelNumber));
		newParcelPanel.setVisible(false);
		mainPanel.setVisible(true);
		repaint();
		validate();
	}

	public AddNewParcelPanel getNewParcelPanel() {
		return newParcelPanel;
	}

	private void createAndShowStatusPanel() {
		deliveryPanel = new JPanel();
		deliveryPanel.setSize(600, 360);
		deliveryPanel.setLayout(null);
		deliveryPanel.setVisible(false);
		deliveryInput = new JTextField();
		checkDelivery = new JButton("Sprawdź");
		deliveryInfo = new JLabel();
		addComponent(deliveryPanel, deliveryInfo, 200, 80, 400, 30);
		addComponent(deliveryPanel, deliveryInput, 225, 120, 150, 25);
		addComponent(deliveryPanel, checkDelivery, 250, 160, 100, 20);
	}

	public void addComponent(JPanel panel, JComponent comp, int x, int y,
			int width, int height) {
		panel.add(comp);
		comp.setBounds(x, y, width, height);
	}

	private JTextArea userInfo;
	private JButton logoutButton;
	private JButton addParcel;
	private JButton showMyParcel;
	private JButton parcelHistory;
	private JButton changeData;
	private JPanel rightPanel;
	private JPanel menuPanel;
	private JPanel mainPanel;
	private Communication db;
	private PersonData loggedInUser;
	private Homepage startPage;
	private JLabel welcomeLabel;
	private JLabel parcelInfo;
	// Panel sprawdzania statusu
	private JPanel deliveryPanel;
	private JButton checkDelivery;
	private JTextField deliveryInput;
	private JLabel deliveryInfo;
	private JButton checkDeliveryStatusButton;

	private AddNewParcelPanel newParcelPanel;
	private ActiveParcelsPanel showParcelPanel;
	private MyDataPanel dataPanel;


	public JPanel getMenuPanel() {
		return menuPanel;
	}

	public void setMenuPanel(JPanel menuPanel) {
		this.menuPanel = menuPanel;
	}

	public JPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(JPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	public Communication getDb() {
		return db;
	}

	public void setDb(Communication db) {
		this.db = db;
	}


	public PersonData getLoggedInUser() {
		return loggedInUser;
	}

	public void setLoggedInUser(PersonData loggedInUser) {
		this.loggedInUser = loggedInUser;
	}


	public JPanel getDeliveryPanel() {
		return deliveryPanel;
	}

	public void setDeliveryPanel(JPanel deliveryPanel) {
		this.deliveryPanel = deliveryPanel;
	}

	
	public ActiveParcelsPanel getShowParcelPanel() {
		return showParcelPanel;
	}

	public void setShowParcelPanel(ActiveParcelsPanel showParcelPanel) {
		this.showParcelPanel = showParcelPanel;
	}

	public MyDataPanel getDataPanel() {
		return dataPanel;
	}

	public void setDataPanel(MyDataPanel dataPanel) {
		this.dataPanel = dataPanel;
	}

	public void setNewParcelPanel(AddNewParcelPanel newParcelPanel) {
		this.newParcelPanel = newParcelPanel;
	}
	
	private ActionListener showActiveParcelListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			showParcels();
			if (mainPanel.isVisible())
				mainPanel.setVisible(false);
			if (newParcelPanel.isVisible())
				newParcelPanel.setVisible(false);
			if (deliveryPanel.isVisible())
				deliveryPanel.setVisible(false);
			if (dataPanel.isVisible())
				dataPanel.setVisible(false);
		}
	};
	public ActionListener getShowActiveParcelListener() {
		return showActiveParcelListener;
	}

	public JTextArea getUserInfo() {
		return userInfo;
	}

	public JButton getLogoutButton() {
		return logoutButton;
	}

	public JButton getAddParcel() {
		return addParcel;
	}

	public JButton getShowMyParcel() {
		return showMyParcel;
	}

	public JButton getParcelHistory() {
		return parcelHistory;
	}

	public JButton getChangeData() {
		return changeData;
	}

	public JPanel getRightPanel() {
		return rightPanel;
	}

	public Homepage getStartPage() {
		return startPage;
	}

	public JLabel getWelcomeLabel() {
		return welcomeLabel;
	}

	public JLabel getParcelInfo() {
		return parcelInfo;
	}

	public JButton getCheckDelivery() {
		return checkDelivery;
	}

	public JTextField getDeliveryInput() {
		return deliveryInput;
	}

	public JLabel getDeliveryInfo() {
		return deliveryInfo;
	}

	public JButton getCheckDeliveryStatusButton() {
		return checkDeliveryStatusButton;
	}

}
